/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.wirelesssecuritymodule.core;

import org.testis.utils.TerminalApplicationExecutor;
import java.io.IOException;

/**
 *
 * @author nurhat
 */
public class WlanApiConnectionChecker extends WirelessConnectionChecker {

    public WlanApiConnectionChecker(String nicGuid) {
        super(nicGuid);
    }

    public boolean isConnected() {
        WpaState currentStatus = getWpaState();
        if (currentStatus == WpaState.COMPLETED) {
            return true;
        } else {
            throw new WirelessSecurityModuleException(
                    WirelessSecurityModuleExceptionType.UNEXPLAINED,
                    "Could not connect to wireless lan. Current status is  " +
                    currentStatus.name());
        }
    }

    public WpaState getWpaState() {
        String state = getValueOf("Interface state");
        if (state != null) {
            WpaState result = WpaState.DISCONNECTED;
             if (state.contains("\"disconnected\"")) {
                result = WpaState.DISCONNECTED;
            }
            else if (state.contains("\"connected\"")) {
                result = WpaState.COMPLETED;
            }
            return result;
        }
        throw new WirelessSecurityModuleException(
                WirelessSecurityModuleExceptionType.UNEXPLAINED,
                "Can not get status of wireless interface...");

    }

    public String runCommand(String command) {
        if (command.equals("status")) {
            command = "qi";
        }
        String output = null;
        TerminalApplicationExecutor executor = new TerminalApplicationExecutor(
                "Wlsample " + command + " " + getDeviceGuid().replace("{", "").replace("}", ""));
        try {
            int exitCode = executor.runExec();
            if (exitCode == 0) {
                output = executor.getOutputString();
            } else {
                throw new WirelessSecurityModuleException(
                        WirelessSecurityModuleExceptionType.UNEXPLAINED,
                        "WlanApiConnectionChecker:Wlsample not running or device is not connected...");
            }
        } catch (IOException ex) {
            throw new WirelessSecurityModuleException(
                    WirelessSecurityModuleExceptionType.UNEXPLAINED,
                    "WlanApiConnectionChecker:Wlsample not found...Error:" + ex.getMessage());
        } catch (InterruptedException ex) {
            throw new WirelessSecurityModuleException(
                    WirelessSecurityModuleExceptionType.UNEXPLAINED,
                    "WlanApiConnectionChecker:Interrupted...Error:" + ex.getMessage());
        }
        return output;
    }

    private String getValueOf(String key) {
        String obtainedOutput = runCommand("qi");
        if (obtainedOutput == null) {
            return null;
        }
        String[] obtainedVals = obtainedOutput.split(System.getProperties().
                getProperty(
                "line.separator"));
        for (int idx = 0; idx < obtainedVals.length; idx++) {
            String[] elem = obtainedVals[idx].split(":");
            if (elem.length > 1) {
                if (elem[0].trim().equals(key)) {
                    return elem[1];
                }
            }
        }
        return null;
    }
}